package com.fadadianqi.logistics.admin.printer;

import com.yeqiangwei.commons.util.StringHelper;
import com.yeqiangwei.commons.util.TypeChange;
import org.apache.log4j.Logger;

import java.awt.*;
import java.awt.print.PageFormat;
import java.awt.print.Printable;

/**
 * 调用打印机功能
 *
 * @author Administrator
 * @see LocalPrinter
 * @deprecated
 */
public class Printer implements Printable {

    private static final Logger log = Logger.getLogger(Printer.class);

    public static void main(String[] args) {

    }

    private int fontSize = 9;
    private static final String FONT_NAME = "宋体";

    private String content = null; // 打印内容

    public Printer(String content) {
        this.content = content;
    }


    @Override
    public int print(Graphics g, PageFormat pf, int page) {
        if (page > 0) {
            return NO_SUCH_PAGE;
        }
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        int ascent = 0; // 缓存每行位置
        String[] linesText = content.split("\n");
        for (String text : linesText) {
            // 字体尺寸
            String _size = StringHelper.getReplacement(text.trim(), "\\[fz(.*?)\\]", "$1", null);
            int size = TypeChange.stringToInt(_size, fontSize); // 当前行文字尺寸

            // 行高
            String _lh = StringHelper.getReplacement(text.trim(), "\\[lh(.*?)\\]", "$1", null);
            int lh = TypeChange.stringToInt(_lh, size);

            ascent += lh;
            Font _font = new Font(FONT_NAME, Font.PLAIN, size);
            g2d.setFont(_font);
            text = text.replaceAll("\\[fz(.*?)\\]", StringHelper.EMPTY).replaceAll("\\[lh(.*?)\\]", StringHelper.EMPTY);
            log.debug("text: " + text); // 每行文本
            g2d.drawString(formatText(text), 0, ascent);
        }
        return PAGE_EXISTS;
    }

    public void setFontSize(int fontSize) {
        this.fontSize = fontSize;
    }

    public String formatText(String text) {
        StringBuilder sb = new StringBuilder();
        String[] txts = text.split("\\[\\/ph\\]");
        for (String t : txts) {
            if (t.indexOf("[ph") != -1) {
                sb.append(_formatText(t + "[/ph]"));
            } else {
                sb.append(t);
            }
        }
        return sb.toString();
    }

    /**
     * 为控制模板位置需要设计占位符标签 placeholder
     * [ph=10][/ph]表示10个占位符
     * [ph=10]ABC[/ph] 打印ABC后面跟7个空格
     */
    private static final String PLACEHOLDER = " "; //1个占位符

    private String _formatText(String text) {
        String _ph = StringHelper.getReplacement(text.trim(), "(.*?)\\[ph=(.*?)\\](.*?)\\[\\/ph\\]", "$2", null);
        String s = StringHelper.getReplacement(text.trim(), "(.*?)\\[ph=(.*?)\\](.*?)\\[\\/ph\\]", "$3", null);
        if (s == null || _ph == null) {
            return text;
        }
        int ph = TypeChange.stringToInt(_ph);
        int n = ph - s.length(); // 规定长度减去字符长度
        String str = text;
        if (n > 0) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < n; i++) {
                sb.append(PLACEHOLDER);
            }
            str = StringHelper.ubbPattern(text, "\\[ph=(.*?)\\](.*?)\\[\\/ph\\]", "$2" + sb.toString(), s);
        } else {
            str = StringHelper.ubbPattern(text, "\\[ph=(.*?)\\](.*?)\\[\\/ph\\]", "$2", s);
        }
        return str;
    }
}
